using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BUS;
using Utility;
using Entities;

public partial class Admin_Danhsachtinhtrangsanpham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();

        }
    }
    private void LoadData()
    {
        Pager.PageSize = 5;
        Criteria c = new Criteria();
        if (txt_tenttsp.Text != "")
        {
            c.add(Condition.AND, tbTrangthaispColumn .Trangthai, Condition.LIKE,
                txt_tenttsp.Text);
        }
        //int total = 0;
        //tbKhuvucCollection  kvuccollection =
        //   tbKhuvucBUS .Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
        //    tbKhuvucColumn.Makv .ToString(), OrderDirection.ASC.ToString(), out total);
        //Repeater1.DataSource = kvuccollection;
        //Repeater1.DataBind();

        //Pager.ItemCount = total;
        int total = 0;
        Criteria cri = new Criteria();
       tbTrangthaiCollection  ttspCollection = tbTrangthaisanphamBUS .Get(cri.Criter, Pager.CurrentIndex, Pager.PageSize,
        tbTrangthaispColumn .Mattsp .ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = ttspCollection;
        Repeater1.DataBind();

        Pager.ItemCount = total;
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                tbTrangthaisanpham  ttspham = tbTrangthaisanphamBUS .GetByID(id);

                tbTrangthaisanphamBUS .Delete(id);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
           tbTrangthaisanpham  ttspham = (tbTrangthaisanpham )e.Item.DataItem;
            Literal lit_tt = (Literal)e.Item.FindControl("lit_tt");


            lit_tt.Text = ttspham.Trangthai;
            //hyp_tenloai.NavigateUrl = "~/Admin/CT_LoaiHang.aspx?id=" + loaihang.maloaigd .ToString();

            //LOAIHANG loaihang = LOAIHANGBUS.GetByID(mathang.Maloai);
            //lit_loaihang.Text = loaihang.Tenloai;

            //img_hinh.ImageUrl = "~/Photos/Thumb/" + mathang.Hinh;

            //NHACUNGCAP nhacc = NHACUNGCAPBUS.GetByID(mathang.Mancc);
            //lit_ncc.Text = nhacc.Tenncc.ToString();

            //NHASANXUAT nhasx = NHASANXUATBUS.GetByID(mathang.Mansx);
            //lit_nsx.Text = nhasx.Tensx;

            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            hpl_Edit.NavigateUrl = "~/Admin/Sua_tinhtrangsanpham.aspx?id=" + ttspham.Mattsp .ToString();
        }

    }
    protected void btn_tthem_Click(object sender, EventArgs e)
    {

        try
        {
           tbTrangthaisanpham  ttspham = new tbTrangthaisanpham ();
            ttspham.Trangthai = txt_tenttsp.Text;

            tbTrangthaisanphamBUS.Insert (ttspham );
            LoadData();

        }
        catch
        {
            Response.Redirect("~/Admin/Danhsachtinhtrangsanpham.aspx");
        }


    }
}
